package com.waoqi.docgen;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;

import java.util.List;
import java.util.Map;

public class Postman2Entity {
    private static final String host = "{{localhost}}";

    // 创建一个模块   一个接口集合属于一个模块
    public static void newModule(String moduleName, List<Map<String, String>> interfaceMap) {
        createPostman(moduleName, interfaceMap);
    }

    //生成POST-Man测试数据（直接使用postman导入ar-food_postman.json文件）
    public static void createPostman(String moduleName, List<Map<String, String>> interfaceMap) {
        Map<String, Object> projectMap = Maps.newHashMap();

        List<Map<String, Object>> listItems = Lists.newLinkedList();
        //name  description collectionId order
        Map<String, Object> infoMap = Maps.newHashMap();
        infoMap.put("_postman_id", ApiDocMain.getPostmanId());
        infoMap.put("name", moduleName);
        infoMap.put("schema", "https://schema.getpostman.com/json/collection/v2.1.0/collection.json");
        projectMap.put("info", infoMap);

        for (Map<String, String> map : interfaceMap) {
            // url  queryParams method   data   name description folder
            Map<String, Object> urlMap = Maps.newHashMap();
            urlMap.put("raw", host + map.get("url"));
            urlMap.put("host", Lists.newArrayList(host));
            urlMap.put("path", Lists.newArrayList(map.get("url").split("/")));
            List<Map<String, Object>> dataMapList = Lists.newLinkedList();
            buildDataParams(dataMapList, map.get("postmanRemark"));
            urlMap.put("query", dataMapList);

            Map<String, Object> requestMap = Maps.newHashMap();
            requestMap.put("method", map.get("method"));
            requestMap.put("header", JSONObject.parseObject(map.get("header"), List.class));
            requestMap.put("url", urlMap);

            Map<String, Object> itemMap = Maps.newHashMap();
            itemMap.put("name", map.get("interfaceName"));
            itemMap.put("request", requestMap);
            itemMap.put("response", Lists.newLinkedList());

            listItems.add(itemMap);
        }
        projectMap.put("item", listItems);
        projectMap.put("protocolProfileBehavior", Maps.newHashMap());
        String result = JSONObject.toJSONString(projectMap, SerializerFeature.WriteMapNullValue);
        System.out.println(result);
        ApiDocMain.appendMethodFile(ApiDocMain.postman_json + moduleName.replaceAll("/", "_") + ".json", result);
    }

    public static void buildDataParams(List<Map<String, Object>> dataMapList, String postmanData) {
        Map<String, Object> resultMap = JSONObject.parseObject(postmanData, Map.class);
        if (MapUtils.isEmpty(resultMap)) {
            return;
        }
        for (String s : resultMap.keySet()) {
            Map<String, Object> dataMap = getDataMap();
            String[] strArr = s.split("#");
            dataMap.put("key", strArr[0]);
            dataMap.put("value", String.valueOf(resultMap.get(s)));
            dataMap.put("type", strArr[1]);
            dataMap.put("description", null);
            if (strArr.length > 2) {
                dataMap.put("description", strArr[2]);
            }
            dataMapList.add(dataMap);
        }
    }

    //接口中的请求参数（post）
    public static Map<String, Object> getDataMap() {
        //key  value   description
        Map<String, Object> requestsMapTmp = Maps.newHashMap();
        requestsMapTmp.put("type", "text");
        requestsMapTmp.put("enabled", true);
        return requestsMapTmp;
    }


}
